/**
 * 安全的工厂方法
 */
// 安全模式创建的工厂类
var Factory = function(type, content) {
    if (this instanceof Factory) {
        var s = new this[type](content);
        return s;
    } else {
        return new Factory(type, content);
    }
}

Factory.prototype = {
    Java: function(content) {
        this.content = content;
        (function(content) {
            var div = document.createElement('div');
            div.innerHTML = content;
            div.style.color = 'green';
            document.getElementById('container').appendChild(div);
        })(content);
    },
    JavaScript: function(content) {
        this.content = content;
        (function(content) {
            var div = document.createElement('div');
            div.innerHTML = content;
            div.style.background = 'pink';
            document.getElementById('container').appendChild(div);
        })(content);
    },
    UI: function(content) {
        this.content = content;
        (function(content) {
            var div = document.createElement('div');
            div.innerHTML = content;
            div.style.border = '1px sold red';
            document.getElementById('container').appendChild(div);
        })(content);
    },
    php: function(content) {
        this.content = content;
        (function(content) {
            var div = document.createElement('div');
            div.innerHTML = content;
            div.style.color = 'yellow';
            div.style.background = 'red';
            document.getElementById('container').appendChild(div);
        })(content);
    }
};

var data = [
    {type: 'JavaScript', content: 'JavaScript 哪家强'},
    {type: 'Java', content: 'Java 哪家强'},
    {type: 'php', content: 'php 哪家强'},
    {type: 'UI', content: 'UI 哪家强'},
    {type: 'UI', content: 'UI 哪家强'},
    {type: 'JavaScript', content: 'JavaScript 哪家强'},
    {type: 'JavaScript', content: 'JavaScript 哪家强'}
];

for (var i = 6; i >= 0; i--) {
    Factory(data[i].type, data[i].content);
}
